/**
 * <p><b>Title:</b> PcMovePrdInfoLogController.java </p>
 * @Package com.wondertek.oes.charge.service.controller
 */
package com.wondertek.poms.log.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import com.wondertek.poms.common.controller.BaseController;
import com.wondertek.poms.common.vo.PageBean;
import com.wondertek.poms.common.vo.ResultBean;
import com.wondertek.poms.core.service.IMoveContLogService;
import com.wondertek.poms.dao.po.MoveContLog;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * <p><b>PcMovePrdInfoLogController Description:</b> 移包日志表Controller层实现</p>
 * @author 窦再兴
 * <p><b>DATE</b> 2019-08-12 20:22:21</p>
 */
@Api("移包日志")
@RestController
@Scope("prototype")
@RequestMapping(value="/move-log")
public class PcMovePrdInfoLogController extends BaseController {
	
	@Autowired
	private IMoveContLogService moveContLogService;
	
	@ApiOperation("移包日志列表")
	@GetMapping("/list")
	@ResponseBody
	public ResultBean list(HttpServletRequest request,
						   MoveContLog moveContLog,
						   @RequestParam(defaultValue = "1") int page,
						   @RequestParam(defaultValue = "20") int rows,
						   @RequestParam(defaultValue = "createdTime") String sidx,
						   @RequestParam(defaultValue = "desc") String sord) {
		Page<MoveContLog> pages = moveContLogService
				.findMoveLogList(moveContLog, page, rows, sord, sidx);

		return PageBean.ok(pages.getTotalPages(), pages.getTotalElements(), pages.getContent());
	}

	@ApiOperation("移包日志新增")
	@PostMapping(value = "/add")
	@ResponseBody
	public ResultBean add(@RequestBody MoveContLog moveContLog) {
		MoveContLog.putTimeAndUser(moveContLog);
		moveContLogService.save(moveContLog);
		return ResultBean.ok(moveContLog);
	}
	
	@ApiOperation("移包日志详情")
	@ApiImplicitParam(name = "id", required = true, paramType = "query", dataType = "Long")
	@GetMapping(value = "/get")
	@ResponseBody
	public ResultBean get(Long id) {
		MoveContLog moveContLog = moveContLogService.get(id);
		return ResultBean.ok(moveContLog);
	}
	
	@ApiOperation("移包日志删除")
	@ApiImplicitParam(name = "id", required = true, paramType = "query", dataType = "Long")
	@DeleteMapping(value = "/del")
	@ResponseBody
	public ResultBean delete(Long id) {
		moveContLogService.deleteById(id);
		return ResultBean.ok();
	}
	
	@GetMapping("/check-move-log")
	public ResultBean checkMoveLog(@RequestParam(required=true) String contentId,
			   @RequestParam(required=false) Long secondPrdInfoPackage,
			   @RequestParam(required=false) Long oldPrdInfoId) {
		if (secondPrdInfoPackage != null && oldPrdInfoId == null) {
			MoveContLog params = new MoveContLog();
			params.setContentId(contentId);
			params.setSecondPrdInfoPackage(secondPrdInfoPackage);
			List<MoveContLog> mpils = moveContLogService.findAll(params, "desc", "createdTime");
			
			if (mpils != null && mpils.size() > 0) {
				for (MoveContLog mpil : mpils) {
					if (mpil.getNewPrdInfoId() != null) {
						return ResultBean.ok(mpils.get(0));
					}
				}
			}
		} else if (secondPrdInfoPackage == null && oldPrdInfoId != null) {
			MoveContLog params = new MoveContLog();
			params.setOldPrdInfoId(oldPrdInfoId);
			List<MoveContLog> mpils = moveContLogService.findAll(params, "desc", "createdTime");
			if (mpils != null && mpils.size() > 0) {
				return ResultBean.ok(mpils.get(0));
			}
		}
		return ResultBean.error();
	}
}


